<template>
  <div>
    <div class="page_box">
      <window-title title="修改密码"></window-title>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm editPass"
      >
        <el-row>
          <el-col :span="12" :offset="6">
            <el-form-item label="原密码：" prop="oldPass">
              <el-input size="small" show-password type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="6">
            <el-form-item label="新密码：" prop="pass">
              <el-input size="small" type="password" show-password v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="6">
            <el-form-item label="确认密码：" prop="checkPass">
              <el-input
                size="small"
                type="password"
                v-model="ruleForm.checkPass"
                autocomplete="off"
                show-password
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="8">
            <el-form-item>
              <el-button size="mini" class="search_btn" @click="submit">提交</el-button>
              <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import windowTitle from "../../components/unit/title";
import CryptoJS from '../../components/utils/secret'
export default {
  components: { windowTitle },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validOldPass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入密码"));
      } else if (value.length != 6) {
        callback(new Error("请输入6位密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldPass: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        oldPass: [{ validator: validOldPass, trigger: "blur" }]
      }
    };
  },

  methods: {
    submit () {
      console.log(11)
      var old = CryptoJS.Encrypt(this.ruleForm.oldPass)
      console.log('加密'+old)
      setTimeout(() => {
        var dec = CryptoJS.Decrypt(old)
        console.log('解密'+dec)
      }, 2000);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.search_btn,
.el-button:hover {
  background-color: #8a70c6;
  color: #ffffff;
  border-color: #8a70c6;
}
.el-form {
  margin-top: 40px;
}
.editPass .el-form-item {
  margin-bottom: 20px;
}
</style>